import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:todo/model/task.dart';
import 'package:todo/pages/detail_task.dart';
import 'package:todo/utils/toast.dart';
import 'package:todo/service/task_dao.dart';

import 'add_task.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
  List<Task> tasks=[];
  TaskDao taskDao;

  final List<Tab> _tabs = <Tab>[
    Tab(text: "全部"),
    Tab(text: "未完成"),
    Tab(text: "已完成"),
  ];
  var _tabController;


  @override
  void initState() {
    super.initState();
    taskDao = TaskDao();

    _tabController = TabController(vsync: this, length: _tabs.length);
    _tabController.addListener((){
      switch(_tabController.index){
        case 0:
          taskDao.tasks().then((value){
            setState(() {
              tasks = value;
            });
          });
          break;
        case 1:
          taskDao.tasksWhere(0).then((value){
            setState(() {
              tasks = value;
            });
          });
          break;
        case 2:
          taskDao.tasksWhere(1).then((value){
            setState(() {
              tasks = value;
            });
          });
          break;
      }
    });

    taskDao.tasks().then((value){
      setState(() {
        tasks = value;
      });
    });
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("任务清单"),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add,size: 35,),
            onPressed: () => _jumpToAddTaskPage(context),
          )
        ],
        bottom: TabBar(
          tabs: _tabs,
          controller: _tabController,
        ),
      ),
      body: ListView.separated (
        itemCount: tasks.length,
        itemBuilder: (BuildContext context, int index){
          return ListTile(
            onTap: () => _jumpToDetailPage(context,index),
            title: _buildContent(index),
            subtitle: Text('截止时间:${tasks[index].endTimeStr}'),
            leading: _buildIsFinish(index),
            trailing: _buildDelete(index),
          );
        },
        separatorBuilder:  (BuildContext context, int index) => Divider(color: Colors.grey[600],),),
    );
  }

  Widget _buildContent(index){
    return Text(
      tasks[index].content,
      style: TextStyle(fontSize: 18),
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
  }
  Widget _buildIsFinish(index){
    return GestureDetector(
      child: tasks[index].isFinish? Icon(Icons.check_circle) : Icon(Icons.check_circle_outline),
      onTap: (){
        tasks[index].isFinish = !tasks[index].isFinish;
        taskDao.updateTask(tasks[index]);
        if(_tabController.index!=0){
          tasks.removeAt(index);
        }
        setState(() {
        });
      },
    );
  }
  Widget _buildDelete(index){
    return IconButton(
      icon:Icon(Icons.delete),
      onPressed: (){
        taskDao.deleteTask(tasks[index].id);
        setState(() {
          tasks.removeAt(index);
        });
        Toast.toast(context,msg: "删除成功！",position: ToastPostion.top);
      },
    );
  }

  void _jumpToAddTaskPage(context){
    Navigator.of(context).push(MaterialPageRoute(
        builder: (ctx) => AddTaskPage()
    )).then((value) {
      taskDao.tasks().then((value){
        setState(() {
          tasks = value;
        });
      });
    });
  }

  void _jumpToDetailPage(context,index){
    Navigator.of(context).push(MaterialPageRoute(
        builder: (ctx) => DetailPage(taskDao, tasks[index])
    )).then((res){
      taskDao.tasks().then((value){
        setState(() {
          tasks = value;
        });
      });
    });
  }
}